package cn.fl.repay.service.impl;

import cn.fl.capital.constant.CapPayInfoConstant;
import cn.fl.repay.constant.InsCstRepayLogConstant;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.repay.dto.InsCstRepayLogDTO;
import cn.fl.repay.condition.InsCstRepayLogQueryCondition;
import cn.fl.repay.service.IInsCstRepayLogApiService;
import cn.fl.repay.service.IInsCstRepayLogService;
import cn.fl.repay.transform.InsCstRepayLogVOTransform;
import cn.fl.repay.vo.InsCstRepayLogVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;
import cmm.mid.core.framework.page.KeyValueVO;

import java.util.List;
import java.util.Map;

/**
 * @Description: 资管客户还款记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsCstRepayLogServiceImpl implements IInsCstRepayLogService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsCstRepayLogServiceImpl.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IInsCstRepayLogApiService insCstRepayLogApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public InsCstRepayLogVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		InsCstRepayLogDTO dto = this.insCstRepayLogApiService.selectInsCstRepayLogById(id);
		return InsCstRepayLogVOTransform.toVO(dto);
	}

	@Override
	public Long add(InsCstRepayLogVO entity) throws BaseException{
		if(null == entity) return null;
		return this.insCstRepayLogApiService.addInsCstRepayLog(InsCstRepayLogVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.insCstRepayLogApiService.removeInsCstRepayLogByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(InsCstRepayLogVO entity) throws BaseException{
		if(null == entity) return false;
		return this.insCstRepayLogApiService.modifyInsCstRepayLog(InsCstRepayLogVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        InsCstRepayLogQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new InsCstRepayLogQueryCondition());
        }
		condition=(InsCstRepayLogQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.insCstRepayLogApiService.searchInsCstRepayLogListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<InsCstRepayLogVO> voList=InsCstRepayLogVOTransform.toVOList((List<InsCstRepayLogDTO>)page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<InsCstRepayLogVO> searchList(InsCstRepayLogQueryCondition condition) throws BaseException{
		if(null == condition) condition=new InsCstRepayLogQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<InsCstRepayLogVO> voList=InsCstRepayLogVOTransform.toVOList(this.insCstRepayLogApiService.searchInsCstRepayLogList(condition));
		this.setDictData(voList);
		return voList;
	}

	/**
	 * 查询数据字典
	 * @param groupCd
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKVList(String groupCd) throws BaseException {
		return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	/**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<InsCstRepayLogVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		Map<String, String> payTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), InsCstRepayLogConstant.CODE_REPAY_TYP_CD);
		for(InsCstRepayLogVO vo:voList){
			if (payTypCdMap != null && payTypCdMap.containsKey(vo.getPayTypCd())) {
				vo.setPayTypCdNm(payTypCdMap.get(vo.getPayTypCd()));
			}
		}
	}

}
